package br.com.dev.tcc.view.sinistro.chain.colisao;

import android.content.Context;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;
import android.widget.RadioButton;
import br.com.dev.tcc.R;
import br.com.dev.tcc.bean.sinistro.GravidadeAcidente;
import br.com.dev.tcc.bean.sinistro.Sinistro;
import br.com.dev.tcc.singleton.SinistroSingleton;
import br.com.dev.tcc.util.EnderecoUtil;
import br.com.dev.tcc.view.sinistro.chain.SinistroChainItem;

public class SinistroColisaoPasso2 extends SinistroChainItem {

	private View txtLocal;
	private EditText valueLocal;
	private View imgGps;
	private RadioButton radioBoSim;
	private RadioButton radioMachucouSim;
	private RadioButton radioLesao;
	private RadioButton radioResgate;
	private View layoutMachucou;
	private View txtBo;
	private EditText valueBo;

	public SinistroColisaoPasso2(Context context) {
		super(context);
		inflateView(R.layout.sinistro_tipo_colisao2);
	}

	@Override
	public void init() {

		txtLocal = view.findViewById(R.id.txtEndereco);
		imgGps = view.findViewById(R.id.ivGps);
		imgGps.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				new EnderecoUtil().searchEndereco(context, valueLocal);
			}
		});
		valueLocal = (EditText) view.findViewById(R.id.txtValorEndereco);
		layoutMachucou = view.findViewById(R.id.layoutMachucou);
		radioMachucouSim = (RadioButton) view
				.findViewById(R.id.radioMachucouSim);
		radioMachucouSim
				.setOnCheckedChangeListener(new OnCheckedChangeListener() {

					@Override
					public void onCheckedChanged(CompoundButton buttonView,
							boolean isChecked) {

						if (isChecked)
							layoutMachucou.setVisibility(View.VISIBLE);
						else
							layoutMachucou.setVisibility(View.GONE);
					}
				});
		txtBo = view.findViewById(R.id.txtNumBo);
		valueBo = (EditText) view.findViewById(R.id.txtValorBo);
		radioBoSim = (RadioButton) view.findViewById(R.id.radioBoSim);
		radioBoSim.setOnCheckedChangeListener(new OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(CompoundButton buttonView,
					boolean isChecked) {

				txtBo.setVisibility(isChecked ? View.VISIBLE : View.GONE);
				valueBo.setVisibility(isChecked ? View.VISIBLE : View.GONE);
			}
		});
		radioLesao = (RadioButton) view.findViewById(R.id.radioLesao);
		radioResgate = (RadioButton) view.findViewById(R.id.radioInternacaoSim);
	}

	@Override
	public boolean isValid() {

		boolean isValid = true;

		if (radioBoSim.isChecked()) {

			if (valueBo.getText().toString().trim().length() < 9) {

				valueBo.setCompoundDrawablesWithIntrinsicBounds(
						null,
						null,
						context.getResources().getDrawable(
								R.drawable.exclamation), null);
				valueBo.requestLayout();
				valueBo.requestFocus();
				isValid = false;
			} else
				valueBo.setCompoundDrawablesWithIntrinsicBounds(null, null,
						null, null);
		}

		if (valueLocal.getText().toString().trim().length() == 0) {

			valueLocal.setCompoundDrawablesWithIntrinsicBounds(null, null,
					context.getResources().getDrawable(R.drawable.exclamation),
					null);
			valueLocal.requestLayout();
			valueLocal.requestFocus();
			isValid = false;
		} else
			valueLocal.setCompoundDrawablesWithIntrinsicBounds(null, null,
					null, null);

		return isValid;
	}

	@Override
	public void setSinistroFields() {

		Sinistro sinistro = SinistroSingleton.getInstance();
		sinistro.setLocal(valueLocal.getText().toString());
		if (radioBoSim.isChecked())
			sinistro.setBo(valueBo.getText().toString());
		else
			sinistro.setBo(null);
		if (radioMachucouSim.isChecked()) {
			sinistro.setAlguemSeMachucou(true);
			sinistro.setGravidadeAcidente(radioLesao.isChecked() ? GravidadeAcidente.LESAO
					: GravidadeAcidente.OBITO);
			sinistro.setInternacaoOuResgate(radioResgate.isChecked());
		} else {
			sinistro.setAlguemSeMachucou(false);
			sinistro.setInternacaoOuResgate(false);
			sinistro.setGravidadeAcidente(null);
		}
	}

	@Override
	public View getFirstView() {
		return txtLocal;
	}

	@Override
	public void onShow() {
		txtLocal.requestFocus();
	}
}
